﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace SNSComponent
{
    public partial class AuthPage : PhoneApplicationPage
    {
        public AuthPage()
        {
            InitializeComponent();

            browser.Navigating+=browser_Navigating;

            browser.IsScriptEnabled = true;

            VerifierUri= default(Uri);
        }

        public Uri VerifierUri{get; private set;}

      

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
          
        }

        public void SetAuthUri(Uri authUri)
        {
            browser.Navigate(authUri);
        }

        public event EventHandler<UserAuthEventArgs> UserAuth;
        
        void browser_Navigating(object sender, NavigatingEventArgs e)
        {
            if (UserAuth == null) return;
            UserAuthEventArgs arg = new UserAuthEventArgs(e.Uri);
            UserAuth(this,arg);
            if (arg.IsAuth == true)
            {
                VerifierUri = e.Uri;
                this.NavigationService.GoBack();

            }
        }
    }

    public class UserAuthEventArgs : EventArgs
    {
        public Uri VerifierUri { get; private set; }

        public bool IsAuth { get; set; }

        public UserAuthEventArgs(Uri verifierUri)
        {
            VerifierUri = verifierUri;
        }
    }

}